package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: sky-take-out
 * @BelongsPackage: com.sky.controller.user
 * @Author: song
 * @CreateTime: 2024-01-16  15:42
 * @Description: 用户地址信息类
 * @Version: 1.0
 */
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端用户地址接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * @description:查询当前登录用户的所有地址信息
     * @author: song
     * @date: 2024/1/16 16:14
     * @param: []
     * @return: com.sky.result.Result<java.util.List<com.sky.entity.AddressBook>>
     **/
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> list(){
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);
        return Result.success(list);
    }
    /**
     * @description:新增地址
     * @author: song
     * @date: 2024/1/16 16:59
     * @param: [addressBook]
     * @return: com.sky.result.Result
     **/
    @PostMapping
    @ApiOperation("新增地址")
    public Result save(@RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return Result.success();
    }
    /**
     * @description:根据用户id查询用户地址
     * @author: song
     * @date: 2024/1/16 17:03
     * @param: [id]
     * @return: com.sky.result.Result<com.sky.entity.AddressBook>
     **/
    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户地址")
    public Result<AddressBook> getById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }
    /**
     * @description:根据id修改用户地址
     * @author: song
     * @date: 2024/1/16 17:06
     * @param: [addressBook]
     * @return: com.sky.result.Result
     **/
    @PutMapping
    @ApiOperation("根据id修改用户地址")
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id){
        addressBookService.deleteById(id);
        return Result.success();
    }

    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);

        if (list !=null && list.size() == 1){
            AddressBook addressBook1 = list.get(0);
            return Result.success(addressBook1);
        }else
            return Result.error("没有查询到默认地址");
    }
}
